package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.util.*;

@SpringBootApplication
@ComponentScan(basePackages = "com.example")
public class BaseProjectApplication {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(BaseProjectApplication.class, args);
        printAllAPIs(context);
    }
    
    /**
     * 打印所有API接口和Controller类信息
     * @param context Spring应用上下文
     */
    private static void printAllAPIs(ApplicationContext context) {
        System.out.println("=================================");
        System.out.println("Available Controllers and APIs:");
        System.out.println("=================================");
        
        // 获取所有Controller类
        Map<String, Object> controllers = context.getBeansWithAnnotation(org.springframework.stereotype.Controller.class);
        Map<String, Object> restControllers = context.getBeansWithAnnotation(RestController.class);
        
        // 合并所有Controller
        Map<String, Object> allControllers = new HashMap<>();
        allControllers.putAll(controllers);
        allControllers.putAll(restControllers);
        
        // 打印Controller信息
        System.out.println("Controllers (" + allControllers.size() + " found):");
        for (Map.Entry<String, Object> entry : allControllers.entrySet()) {
            String beanName = entry.getKey();
            Object controller = entry.getValue();
            Class<?> controllerClass = controller.getClass();
            
            System.out.println("  " + controllerClass.getSimpleName() + " (" + beanName + ")");
        }
        
        System.out.println("\nAPI Endpoints:");
        RequestMappingHandlerMapping mapping = context.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = mapping.getHandlerMethods();
        
        // 按Controller类分组显示API
        Map<Class<?>, List<Map.Entry<RequestMappingInfo, HandlerMethod>>> groupedByController = new HashMap<>();
        
        for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
            Class<?> controllerClass = entry.getValue().getBeanType();
            groupedByController.computeIfAbsent(controllerClass, k -> new ArrayList<>()).add(entry);
        }
        
        // 打印每个Controller的API接口
        for (Map.Entry<Class<?>, List<Map.Entry<RequestMappingInfo, HandlerMethod>>> group : groupedByController.entrySet()) {
            Class<?> controllerClass = group.getKey();
            List<Map.Entry<RequestMappingInfo, HandlerMethod>> apis = group.getValue();
            
            System.out.println("\nController: " + controllerClass.getSimpleName());
            for (Map.Entry<RequestMappingInfo, HandlerMethod> api : apis) {
                RequestMappingInfo requestMappingInfo = api.getKey();
                HandlerMethod handlerMethod = api.getValue();
                
                System.out.println("  " + requestMappingInfo.getPatternsCondition() + 
                                 " " + requestMappingInfo.getMethodsCondition());
            }
        }
        
        System.out.println("\n=================================");
        System.out.println("Total endpoints: " + handlerMethods.size());
        System.out.println("Total controllers: " + allControllers.size());
        System.out.println("=================================");
    }
}